/*
 * Copyright (C) 2009 Leandro de Oliveira Aparecido <lehphyro@gmail.com>
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.google.code.japa.engine;

import java.util.*;

import com.google.code.japa.repository.collection.*;

/**
 * Represents an object processed by JAPA.
 * 
 * @author Leandro Aparecido
 * @since 1.0
 * @see com.google.code.japa.engine.JapaEngine#processObject(Object)
 */
public class JapaObject {
	/**
	 * Object processed by Japa.
	 */
	private Object target;
	
	/**
	 * Persistent collections injected by Japa into the {@link #target}.
	 */
	private List<PersistentCollection<?>> persistentCollections;

	/**
	 * Constructs an empty JapaObject with no persistent collections.
	 */
	public JapaObject() {
		persistentCollections = Collections.emptyList();
	}

	/**
	 * Constructs a JapaObject with the required parameters.
	 * 
	 * @param target Object processed.
	 * @param persistentCollections Persistent collections belonging to the target object.
	 */
	public JapaObject(Object target, List<PersistentCollection<?>> persistentCollections) {
		this.target = target;
		this.persistentCollections = new ArrayList<PersistentCollection<?>>(persistentCollections);
	}

	/**
	 * Flushes the persistent collections of the target object.
	 */
	public void flush() {
		for (PersistentCollection<?> persistentCollection : persistentCollections) {
			persistentCollection.flush();
		}
	}

	// Getters
	
	public Object getTarget() {
		return target;
	}

	public List<PersistentCollection<?>> getPersistentCollections() {
		return Collections.unmodifiableList(persistentCollections);
	}

}
